<template>
  <!--
      作者：yunding
      时间：2019-10-24
      描述：商品管理
  -->
  <div class="product-list">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="出售中" name="sell">
          <span slot="label">出售中 <el-tag size="mini">{{ product_count.sell }}</el-tag></span>
        </el-tab-pane>
        <el-tab-pane label="仓库中" name="lower">
          <span slot="label">仓库中 <el-tag size="mini">{{ product_count.lower }}</el-tag></span>
        </el-tab-pane>
        <el-tab-pane label="回收站" name="recovery">
          <span slot="label">回收站 <el-tag size="mini">{{ product_count.recovery }}</el-tag></span>
        </el-tab-pane>
      </el-tabs>
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="商品分类">
          <el-cascader v-model="searchForm.category_id" :options="categoryList"
            :props="{ label: 'name', value: 'category_id', children: 'child' }" @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="商品名称"><el-input size="small" v-model="searchForm.product_name"
            placeholder="请输入商品名称"></el-input></el-form-item>
        <el-form-item label="门店">
          <el-select size="small" v-model="searchForm.shop_supplier_id" placeholder="请选择" filterable>
            <el-option label="全部" value="0"></el-option>
            <el-option v-for="(item, index) in storeList" :key="index" :label="item.name"
              :value="item.shop_supplier_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="product_name" label="产品" width="400px">
            <template slot-scope="scope">
              <div class="product-info">
                <div class="pic"><img v-img-url="scope.row.image[0].file_path" alt="" /></div>
                <div class="info">
                  <div class="name">{{ scope.row.product_name }}</div>
                  <div class="price">¥ {{ scope.row.product_price }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="supplier.name" label="商户名称"></el-table-column>
          <el-table-column prop="category.name" label="分类"></el-table-column>
          <el-table-column prop="sales_actual" label="实际销量"></el-table-column>
          <el-table-column prop="product_stock" label="库存"></el-table-column>
          <el-table-column prop="view_times" label="点击数"></el-table-column>
          <el-table-column prop="product_status.text" label="商品属性">
            <template slot-scope="scope">
              <span v-if="scope.row.is_virtual == 0">实物商品</span>
              <span v-if="scope.row.is_virtual == 1" class="green">虚拟商品</span>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="发布时间"></el-table-column>
          <el-table-column prop="product_sort" label="排序"></el-table-column>
          <el-table-column fixed="right" label="操作" width="80">
            <template slot-scope="scope">
              <div class="table-btn-column">
                <el-button v-if="scope.row.audit_status == 10" @click="editClick(scope.row)" type="text" size="small"
                  v-auth="'/product/product/edit'">编辑商品</el-button>
                <el-button @click="delClick(scope.row)" type="text" size="small"
                  v-auth="'/product/product/delete'">删除商品</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
        :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"></el-pagination>
    </div>
    <!-- 云仓编辑 -->
    <el-dialog title="添加云仓商品" :visible.sync="dialogFormVisible">
      <el-form :model="addForm" size="small" ref="addForm" label-width="100px">
        <el-form-item label="溢价比例" :rules="[{ required: true, message: '请输入溢价比例' }]" prop="yun_rate">
          <el-input v-model="addForm.yun_rate" placeholder="请输入溢价比例">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="划线价格" :rules="[{ required: true, message: '请输入划线价格' }]" prop="line_price">
          <el-input v-model="addForm.line_price" placeholder="请输入划线价格">
          </el-input>
        </el-form-item> -->
        <el-form-item label="初始销量" :rules="[{ required: true, message: '请输入初始销量' }]" prop="sales_initial">
          <el-input v-model="addForm.sales_initial" placeholder="请输入初始销量">
          </el-input>
        </el-form-item>
        <el-form-item label="所属分类" :rules="[{ required: true, message: '请选择所属分类' }]" prop="category_id">
          <el-cascader v-model="addForm.category_id" :options="categoryList" @change="handleChange1"
            :props="{ label: 'name', value: 'category_id', children: 'child' }"></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit_yun">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';
export default {
  components: {},
  data() {
    return {
      /*切换菜单*/
      activeName: 'sell',
      /*切换选中值*/
      activeIndex: '0',
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*搜索参数*/
      searchForm: {
        product_name: '',
        category_id: '',
        is_yun: 1,
        shop_supplier_id: '',
      },
      /*列表数据*/
      tableData: [],
      /*全部分类*/
      categoryList: [],
      storeList: [],
      /*商品统计*/
      product_count: {},
      dialogFormVisible: false,
      addForm: {
        yun_rate: '',
        // line_price: '',
        sales_initial: 0,
        category_id: '',
        product_id: '',
      },
    };
  },
  created() {
    /*获取列表*/
    this.getData();
    this.getData1()
  },
  methods: {
    handleChange1(e) {
      this.addForm.category_id = e[e.length - 1];
    },
    edit_yun() {
      let self = this
      self.$refs.addForm.validate(valid => {
        if (valid) {
          PorductApi.editProduct1(self.addForm, true).then(data => {
            self.$message({
              message: '编辑成功',
              type: 'success'
            });
            self.dialogFormVisible = false
            self.getData();
          })
        } else {
          self.$message({
            message: '请检查必填项是否填写完整',
            type: 'error'
          });
        }
      });
    },
    getData1() {
      let self = this;
      PorductApi.getStoreList({}, true).then(data => {
        self.storeList = data.data
      })
    },
    handleChange(e) {
      this.searchForm.category_id = e[e.length - 1]
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.loading = true;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },

    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },

    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Params.type = self.activeName;
      self.loading = true;
      PorductApi.productList(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.categoryList = data.data.category;
          self.totalDataNumber = data.data.list.total;
          self.product_count = data.data.product_count;
        })
        .catch(error => {
          self.loading = false;
        });
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    /*打开编辑*/
    editClick(row) {
      this.addForm.yun_rate = row.yun_rate
      this.addForm.line_price = row.line_price
      this.addForm.sales_initial = row.sales_initial
      this.addForm.category_id = row.category_id
      this.addForm.product_id = row.product_id
      this.dialogFormVisible = true
    },
    /*删除*/
    delClick: function (row) {
      let self = this;
      self
        .$confirm('删除后将移动到回收站，确定删除该记录吗?', '提示', {
          type: 'warning'
        })
        .then(() => {
          PorductApi.delProduct({
            product_id: row.product_id
          }).then(data => {
            self.$message({
              message: '删除成功',
              type: 'success'
            });
            self.getData();
          });
        });
    },
    /*修改状态*/
    changeStatus(item, value) {
      let self = this;
      let msg = '确认要移动该商品到仓库吗?';
      self.$confirm(msg, '提示', {
        type: 'warning'
      })
        .then(() => {
          PorductApi.changeStatus({
            product_id: item.product_id,
            state: value
          }).then(data => {
            self.$message({
              message: '操作成功',
              type: 'success'
            });
            self.getData();
          });
        });
    },
  }
};
</script>

<style></style>
